About
RSS

Bit Focus


std::unique_ptr<cookbook>

    std::unique_ptr 是 C++11 标准的 STL 中, 用来取代 std::auto_ptr 的指针容器 (如果在代码中使用了 std::auto_ptr, 使用 gcc 4.5 之后的版本加上 -std=c++0x -Wall 参数编译时, gcc 会报出警告 std::auto_ptr 已经过时).

    同 std::auto_ptr 一样, std::unique_ptr 的基本功效也能当作简单的自动对象管理指针来使用, 如
#include <memory> /* for std::unique_ptr */
#include <iostream>

struct echo {
    echo()
    {
        std::cout << "ctor" << std::endl;
    }

    ~echo()
    {
        std::cout << "dtor" << std::endl;
    }

    echo(echo const&) = delete;
};

void test()
{
    std::unique_ptr<echo> echo_ptr(new echo);
}

int main()
{
    test();
    std::cout << "." << std::endl;
    return 0;
}
输出应该如
ctor
dtor
.
    类型 echo 在构造和析构时的输出信息可以帮助掌握对象的生命周期. 如上述程序中, echo 对象在 test 函数结束时析构.

    std::unique_ptr 的复制构造函数被 delete 处理了, 所以类似下面的代码会出错
std::unique_ptr<int> a(new int(0));
std::unique_ptr<int> b(a); // ERROR
            // deleted function
            // ‘std::unique_ptr<...>::unique_ptr(std::unique_ptr<...> const&)'
    如果确实需要复制一份整数的值, 需要可以做如下操作
std::unique_ptr<int> b(new int(*a));
    而如果是想要将 a 所持有的指针转移到 b 中, 需要显式调用 std::move 将来移交指针控制权
std::unique_ptr<int> a(new int(0));
std::unique_ptr<int> b(std::move(a));
    此时 a 中的指针会变为 NULL. 明确的转移语义由 std::move 函数表达, 与 std::auto_ptr 暧昧的构造函数不同, std::move 明确指出 std::unique_ptr 对象是通过转移构造进行参数传递的.

    当函数需要使用 std::unique_ptr 作为参数或返回值时, 可以是如下几种形式

Permanent Link: /p/181 Load full text

Post tags:

 C++
 C++11
 unique_ptr
 Move Semantic
 Tutorial

django 架设网站入门指南[贰]

上节回顾 – ORM 和数据库

创建 admin 应用

    打开 guestbook/urls.py 仔细研究一下注释, 会看到有几行写着, 取消注释来激活 admin. django 的 admin 应用是一个非常不错的后台. 现在就来开启它吧
from django.conf.urls.defaults import *

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Example:
    # (r'^guestbook/', include('guestbook.foo.urls')),

    # Uncomment the admin/doc line below and add 'django.contrib.admindocs'
    # to INSTALLED_APPS to enable admin documentation:
    # (r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    (r'^admin/', include(admin.site.urls)),

    (r'^$', 'guestbook.home.views.display'),
    (r'^echo/$', 'guestbook.home.views.echo'),
    (r'^save/', 'guestbook.home.views.save_message'),
)
    因为是应用, 所以呢, 还得去 settings.py 登记
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.admin',
    'guestbook.home',
)
    最后要记得执行数据库同步来创建 admin 相关的表
$ guestbook/manage.py syncdb
    现在转到 http://localhost:8000/admin/ 会看到登录提示页面啦. 现在填入第一次 syncdb 时输入的帐号密码就能登入了. 不过现在的 admin 还很挫, 甚至不能管理留言. 进入 guestbook/home 目录, 创建 admin.py 源文件, 加入如下内容
from guestbook.home.models import Message
from django.contrib import admin

admin.site.register(Message)
    刷新页面, 唉, 没有反应, 因为服务器仅监视那些改动过的文件并重新载入, 这次是单独添加的文件所以没有自动重置, 所以得重启服务器才行. 然后, 就能在 admin 应用中看到留言对象并修改它们了.

定制 admin

Permanent Link: /p/77 Load full text

Post tags:

 django
 Tutorial
 Web Server
 Python

django 架设网站入门指南[壹]

上节回顾 – 配置 基本视图和逻辑

数据库配置

    仍然在 settings.py 中, 找到 DATABASE_ 开头的项目. 现在用 sqlite3 作为数据库, 它已经集成在 python 2.5 以后的版本中, 这样就省去了安装配置的环节. 现在修改这些项目
DATABASE_ENGINE = 'django.db.backends.sqlite3'
DATABASE_NAME = 'guestbook/db_file'
这里 DATABASE_NAME 同样需要给出文件的绝对路径, 原理跟之前模板目录那个一样, 如果不能确定每次的工作目录, 那么就填绝对 db_file 的路径吧. 保存设置, 然后执行数据库同步操作
$ guestbook/manage.py syncdb
会输出提示
Creating table auth_permission
Creating table auth_group
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no):
这些是 django 为内置的用户, 会话等类型建立的表, 最后询问是否立即建立一个超级用户. 这里答 yes (仅一个 y 是不行的唉), 然后输入用户名, 电子邮箱和密码. 最后会建立表的索引, 然后结束. 现在, 你可以在 guestbook 目录下看到文件 db_file 了, 它是数据库使用的文件.

ORM 和数据库操作

    django 的 ORM 非常轻松. 打开 home 目录下的 models.py, 建立留言类型
from django.db import models

class Message(models.Model):
    name = models.CharField(max_length=32)
    content = models.CharField(max_length=32)
    dt = models.DateTimeField(auto_now=True)
注, 使用 django 的 ORM, 该类型需要继承于 models.Model, 而要往数据库中放的数据项, 需要是 models 模块中的对应的域类型. 至于 auto_now=True, 它指出时间和日期在该对象插入数据库时自动使用当前日期时间.
    类型弄好了, 得再同步一次数据库. 这些操作可以不重启服务器
$ guestbook/manage.py sql home
$ guestbook/manage.py syncdb
    回到 guestbook/home/views.py, 增加函数 save_message, 让它将表单数据存入数据库, 并重定向回到首页

Permanent Link: /p/76 Load full text

Post tags:

 Web Server
 Tutorial
 Python
 django
 ORM

django 架设网站入门指南[零]

安装

    根据 django 官方文档, 需要 python 2.3 或更高版本. 不过按现在的 python 普及程度, 想必大家的机器上都有 python 2.5 或 2.6 了. 我的操作系统是 ubuntu 9.10, 自带的是 2.6 版本.
    debian 用户安装 django 很简单, 只需要
# apt-get install python-django
就可以了, 其它发行版看有没有类似的方法. 或者在官方站点下载源代码安装
    祝好运.

建立项目 startproject

    首先找到一个地方, 建立一个项目 guestbook, 我们将在这个项目中搭建一个留言板.
$ django-admin startproject guestbook
有些系统中 django-admin 并非一个命令, 需要
$ python django-admin.py startproject guestbook
    执行完这个之后, 当前目录下会创建一个名为 guestbook 的目录, 里面包含了一个最简单的服务器: 显示一个主页. 执行
$ python guestbook/manage.py runserver
启动后会看到类似以下输出
Validating models...
0 errors found

Django version 1.1.1, using settings 'guestbook.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
这时, manage.py 已经在本地 8000 端口创建了一个服务器, 赶快打开浏览器去访问吧!

    为 guestbook/manage.py 加上可执行属性 (在 ubuntu 9.10 下该属性已经自动添加了)
$ chmod u+x guestbook/manage.py

创建应用 startapp

    在 django 世界的哲学里, 每个功能称之为一个应用. 创建应用也使用 manage.py 来完成. 比如现在用以下命令创建一个首页应用
$ guestbook/manage.py startapp home
这时 guestbook 目录下会多出一个 home 目录, 里面有这些文件
__init__.py
models.py
tests.py
views.py
    比较烦的是, 这时得回去修改设置, 打开 settings.py 查找 INSTALLED_APPS, 在这一坨里面加一行
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'guestbook.home',
)
    这样 home 应用才算是有了名分. 好, 现在再来实现功能. 因为现在只是处理页面, 所以只需要在 guestbook/home/views.py 里面修修补补就好了. 打开它, 其实相当于是个空文件, 把这一段代码放进去
from django.http import HttpResponse

def display(req):
    return HttpResponse('<html><body>hello, django!')
    我知道你已经不耐烦了, 如果你曾经写 php 的话. 不过现在还有最后一步, 修改路径映射. 打开 guestbook/urls.py, 在里面加一句话

Permanent Link: /p/74 Load full text

Post tags:

 Web Server
 Tutorial
 django
 Python

0 1 Page 2


. Back to Bit Focus
NijiPress - Copyright (C) Neuron Teckid @ Bit Focus
About this site